<?php

class ProductSerial extends AppModel {

    var $name = 'ProductSerial';
    var $useTable = 'products_serials';
    var $belongsTo = array(
        'Location', 'CustLocation', 'Product'
    );

    function beforeSave() {
        // มาจาก ITS Dispatch
        if (isset($this->data['ProductSerial']['quote_price_valid_to'])) {
            $this->data['ProductSerial']['quote_price_valid_to'] = $this->dateFormatBeforeSave($this->data['ProductSerial']['quote_price_valid_to']);
            $this->data['ProductSerial']['price_adjustment_1_date'] = $this->dateFormatBeforeSave($this->data['ProductSerial']['price_adjustment_1_date']);
            $this->data['ProductSerial']['price_adjustment_1_valid_to'] = $this->dateFormatBeforeSave($this->data['ProductSerial']['price_adjustment_1_valid_to']);
            $this->data['ProductSerial']['price_adjustment_2_date'] = $this->dateFormatBeforeSave($this->data['ProductSerial']['price_adjustment_2_date']);
            $this->data['ProductSerial']['price_adjustment_2_valid_to'] = $this->dateFormatBeforeSave($this->data['ProductSerial']['price_adjustment_2_valid_to']);
            $this->data['ProductSerial']['cust_purchase_date'] = $this->dateFormatBeforeSave($this->data['ProductSerial']['cust_purchase_date']);
        }

        return true;
    }

    function afterFind($results) {
        if (is_array($results)) {
            foreach ($results as &$ps) {
                if (isset($ps['ProductSerial']['quote_price_valid_to'])) {
                    $ps['ProductSerial']['quote_price_valid_to'] = $this->dateFormatAfterFind($ps['ProductSerial']['quote_price_valid_to']);
                    $ps['ProductSerial']['price_adjustment_1_date'] = $this->dateFormatAfterFind($ps['ProductSerial']['price_adjustment_1_date']);
                    $ps['ProductSerial']['price_adjustment_1_valid_to'] = $this->dateFormatAfterFind($ps['ProductSerial']['price_adjustment_1_valid_to']);
                    $ps['ProductSerial']['price_adjustment_2_date'] = $this->dateFormatAfterFind($ps['ProductSerial']['price_adjustment_2_date']);
                    $ps['ProductSerial']['price_adjustment_2_valid_to'] = $this->dateFormatAfterFind($ps['ProductSerial']['price_adjustment_2_valid_to']);
                    $ps['ProductSerial']['cust_purchase_date'] = $this->dateFormatAfterFind($ps['ProductSerial']['cust_purchase_date']);
                }
            }
        }
        return $results;
    }

    /**
     * คัดลอกข้อมูลจาก Product Serial อันเก่า มาใส่ Product Serial อันใหม่
     * update active flag PS เก่า เป็น false
     * ใช้ในกรณี Cust Return กลับเข้า stock, Cancel Reserve,...
     * @param <type> $id
     */
    function copyProductSerial($id, $cust_stock_in_id = null, $cust_stock_in_type = null) {
        $is_success = true;

        // ดึงรายการ ProductSerial ที่ stock out ออกไปแล้วกำลังจะ return เรียงจากรายการหลังสุด
        $this->contain();
        $ps = $this->findById($id);

        $data = array();
        $data['ProductSerial']['id'] = null;
        $data['ProductSerial']['product_id'] = $ps['ProductSerial']['product_id'];
        $data['ProductSerial']['cust_product_site_id'] = $ps['ProductSerial']['cust_product_site_id'];
        $data['ProductSerial']['serial_no'] = $ps['ProductSerial']['serial_no'];
        $data['ProductSerial']['other_information'] = $ps['ProductSerial']['other_information'];
        $data['ProductSerial']['cost'] = $ps['ProductSerial']['cost'];
        $data['ProductSerial']['price'] = $ps['ProductSerial']['price'];
        $data['ProductSerial']['its_stock_in_type'] = $ps['ProductSerial']['its_stock_in_type'];
        $data['ProductSerial']['its_stock_in_id'] = $ps['ProductSerial']['its_stock_in_id'];
        $data['ProductSerial']['its_stock_out_type'] = $ps['ProductSerial']['its_stock_out_type'];
        $data['ProductSerial']['its_stock_out_id'] = $ps['ProductSerial']['its_stock_out_id'];
        $data['ProductSerial']['stock_out_priority'] = $ps['ProductSerial']['stock_out_priority'];

        if (!empty($ps['ProductSerial']['cust_location_id'])) {
            $data['ProductSerial']['cust_location_id'] = $ps['ProductSerial']['cust_location_id'];
        } else {
            $data['ProductSerial']['cust_location_id'] = null;
        }

        // ถ้าไม่ได้ระบุ cust_stock_in_type เข้ามาใช้ตามเดิม
        if ($cust_stock_in_type == null) {
            $cust_stock_in_type = $ps['ProductSerial']['cust_stock_in_type'];
        }
        if ($cust_stock_in_id == null) {
            $cust_stock_in_id = $ps['ProductSerial']['cust_stock_in_type'];
        }

        $data['ProductSerial']['cust_stock_in_type'] = $cust_stock_in_type;
        $data['ProductSerial']['cust_stock_in_id'] = $cust_stock_in_id;
        $data['ProductSerial']['quote_price_valid_to'] = $ps['ProductSerial']['quote_price_valid_to'];
        $data['ProductSerial']['price_adjustment_case'] = $ps['ProductSerial']['price_adjustment_case'];
        $data['ProductSerial']['price_adjustment_1_date'] = $ps['ProductSerial']['price_adjustment_1_date'];
        $data['ProductSerial']['price_adjustment_1_valid_to'] = $ps['ProductSerial']['price_adjustment_1_valid_to'];
        $data['ProductSerial']['price_adjustment_2_date'] = $ps['ProductSerial']['price_adjustment_2_date'];
        $data['ProductSerial']['price_adjustment_2_valid_to'] = $ps['ProductSerial']['price_adjustment_2_valid_to'];
        $data['ProductSerial']['cust_purchase_date'] = $ps['ProductSerial']['cust_purchase_date'];

        // update ProductSerial เดิมว่าใช้งานไม่ได้
        $this->id = $id;
        $is_success &= $this->saveField('is_active_flag', false);

        // บันทึก ProductSerial อันใหม่
        $this->create();
        $is_success &= $this->save($data);
        return $is_success;
    }

    /**
     * หา Price จาก Product Serial ราคาถัดไปที่ยังไม่ถูก stock out
     * @param <type> $cust_product_site_id
     */
    function findCustPrice($cust_product_site_id) {
        return $this->field('price', array(
                    'cust_product_site_id' => $cust_product_site_id,
                    'cust_stock_in_id !=' => null,
                    'cust_stock_out_id IS NULL',
                    'is_active_flag', true
                ),
                array('stock_out_priority')
        );
    }

}

?>